package com.hexgon.plugins.rabbitmq;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.stereotype.Component;

/**
 * MultiRabbitTemplate 多个 RbbitTemplate 统一管理
 * @author: XuZhibin
 * @date: 2023/11/12 12:29
 * @version: v1.0
 * @attention:
 */
@Component
public class MultiRabbitTemplate {

    @Autowired
    private DefaultListableBeanFactory defaultListableBeanFactory;

    /**
     * 根据名称获取 RabbitMQ源
     * @param rabbitInstanceName
     * @return
     */
    public RabbitTemplate rabbitInstance(String rabbitInstanceName) {
        String beanName = rabbitInstanceName + "RabbitTemplate";
        boolean existTemplate = defaultListableBeanFactory.containsBean(beanName);
        if (!existTemplate) {
            throw new IllegalArgumentException("无效的RabbitMQ实例名称");
        }
        return defaultListableBeanFactory.getBean(beanName, RabbitTemplate.class);
    }

}
